package com.ruoyi.web.controller.jl;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.base.Result;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.jl.domain.Player;
import com.ruoyi.jl.domain.custome.CustomPlayer;
import com.ruoyi.jl.mapper.PlayerMapper;
import com.ruoyi.jl.service.IPlayerService;
import com.ruoyi.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 玩家 控制层
 * 
 * @author wh
 * @date 2019-04-29
 */
@Slf4j
@Controller
@RequestMapping("/jl/player")
public class PlayerController extends BaseController {
    private String prefix = "jl/player";
	
	private final IPlayerService playerService;

	@Autowired
	PlayerMapper playerMapper;

	@Autowired
	public PlayerController(IPlayerService playerService) {
		this.playerService = playerService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("jl:player:view")
	@GetMapping
	public String player(){
	    return prefix + "/player";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{playerId}")
	public String edit(@PathVariable("playerId") Integer playerId, ModelMap mmap){
		CustomPlayer player = playerService.queryEditInfo(playerId);
		mmap.put("player", player);
		return prefix + "/edit";
	}
	
	/**
	 * 查询玩家列表
	 */
	@RequiresPermissions("jl:player:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(Player player){
		startPage();
		List<CustomPlayer> list = playerService.list(player);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存玩家
	 */
	@RequiresPermissions("jl:player:add")
	@Log(title = "玩家", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(Player player){
		playerService.queryPlayerInfo(player);
		return Result.success();
	}

	/**
	 * 修改保存玩家
	 */
	@RequiresPermissions("jl:player:edit")
	@Log(title = "玩家", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(CustomPlayer player){
		playerService.updatePlayer(player);
		return Result.success();
	}
	
	/**
	 * 删除玩家
	 */
	@RequiresPermissions("jl:player:remove")
	@Log(title = "玩家", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		playerService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出玩家列表
	 */
	@RequiresPermissions("jl:player:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(Player player){
		/*List<Player> list = playerService.list(player);
		ExcelUtil<Player> util = new ExcelUtil<>(Player.class);
		util.exportExcel(list, "player");*/
		return Result.success();
	}

	@Log(title = "修改状态", businessType = BusinessType.UPDATE)
	@RequiresPermissions("jl:player:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(Player player) {
		return toAjax(playerService.changeStatus(player));
	}

	/**
	 * 添加金币回显
	 * @param playerId
	 * @param mmap
	 * @return
	 */
	@RequiresPermissions("jl:player:addGold")
	@Log(title = "添加金币", businessType = BusinessType.UPDATE)
	@GetMapping("/addGold/{playerId}")
	public String addGold(@PathVariable("playerId") Long playerId, ModelMap mmap) {
		Player player = playerService.getBaseMapper().selectById(playerId);
		SysUser sysUser = ShiroUtils.getSysUser();
		mmap.put("sysUser", sysUser);
		mmap.put("player", player);
		return prefix + "/addGold";
	}

	@RequiresPermissions("jl:player:addGold")
	@Log(title = "添加金币", businessType = BusinessType.UPDATE)
	@PostMapping("/addGold")
	@ResponseBody
	public AjaxResult addGoldSave(HttpServletRequest request, @RequestParam("adminId") Integer adminId,
								  @RequestParam("playerId") Integer playerId, @RequestParam("goldNum") Integer goldNum) {
		String ipAddr = IpUtils.getIpAddr(request);
		playerService.giveGold(adminId, playerId, goldNum, ipAddr, 0);
		return AjaxResult.success();
	}
}
